package com.flute.icrawler.app.result;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.flute.haflute.monitor.GeneralStatusCenter;
import com.flute.haflute.monitor.StatusMessage;
import com.flute.icrawler.app.entity.CrawlResult;
import com.flute.icrawler.app.service.CrawlService;

/**
 * CrawlResult处理线程 从数据池中取数据存储到Cassandra数据库中
 * 
 * 
 */
public class CrawlResultDealThread extends Thread {

	private static final Logger LOGGER = LoggerFactory
			.getLogger(CrawlResultDealThread.class);

	private boolean isFinished = false;
	
	private IStoreable storeable = null;

	public CrawlResultDealThread(IStoreable storeable) {
		// TODO Auto-generated constructor stub
		this.setName("CrawlResultDealThread");
		this.storeable = storeable;

	}

	public void setFinished(boolean isFinished) {
		this.isFinished = isFinished;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub

		while (!(isFinished && CrawlService.getInstance().getCrawlResultPool()
				.size() == 0)) {

			innerStart();

		}

	}

	private void innerStart() {

		// TODO Auto-generated method stub
		LOGGER.debug("CrawlResultDealThread:{}", "innerStart");

		List<CrawlResult> crawlResults = CrawlService.getInstance()
				.getCrawlResultPool().removeCrawlResults();

		if (null == crawlResults) {

			LOGGER.info("CassandraStore:crawlResults is null");
			return;
		}
		// IStoreable storeable =
		// StoreImpFactory.getFileStore(CrawlConfigUtil.getBaseConfig());

		if (null == storeable) {
			StatusMessage statusMessage = new StatusMessage();
			statusMessage.topic = "Cassandra存储异常";
			statusMessage.content = "存储句柄为空";

			LOGGER.error("CassandraStoreImp:{}", "连接数据库失败");
			GeneralStatusCenter.getInstance().report(statusMessage);
			return;
		}
		int size = crawlResults.size();
		for (int i = 0; i < size; i++) {

			CrawlResult crawlResult = crawlResults.get(i);
			if (LOGGER.isDebugEnabled()) {
				LOGGER.debug("CrawlResultDealThread:{}", "LocalMirrorStore:"
						+ "getCharSet=" + crawlResult.getCharSet()
						+ "getContentType=" + crawlResult.getContentType()
						+ "hasContent=" + (crawlResult.getContent() == null));
			}

			// 成功获取 将内容保留下来
			storeable.store(crawlResult);
		}

		storeable.close();

		LOGGER.info("CrawlResultDealThread: {} url have been stored", size);

	}

}
